package com.juno.cafenoa.mypage;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.juno.cafenoa.Constants;
import com.juno.cafenoa.R;
import com.juno.cafenoa.TopNavigatorView;
import com.juno.cafenoa.net.AsyncTaskCompleteListener;
import com.juno.cafenoa.net.HttpPostTask;
import com.juno.cafenoa.util.CafeNoaPreference;
import com.juno.cafenoa.util.Util;
import com.juno.ui.uitableview.UITableView;
import com.juno.ui.uitableview.UITableView.ClickListener;

public class MyOrderList extends Activity {
	UITableView mTableView = null;
	TopNavigatorView navi = null;
	Context mContext = null;
	TextView noList = null;
	
	ArrayList<OrderList> list;
	
	ProgressBar pb = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_myorder_list);
		navi = (TopNavigatorView) findViewById(R.id.navi_myorderpage);
		mTableView = (UITableView) findViewById(R.id.myorderListTableView);
		pb = (ProgressBar) findViewById(R.id.myorderlist_pb);
		noList = (TextView) findViewById(R.id.no_order_list);
		
		
		navi.init("MY ORDER LIST");
		mContext = this;
		pb.setVisibility(View.VISIBLE);
		mTableView.setVisibility(View.GONE);
		list = new ArrayList<OrderList>();
		CustomClickListener listener = new CustomClickListener();
		mTableView.setClickListener(listener);
		getMyOrderListener.excuteHttpTask();
	}
	
	protected UITableView getUITableView(){
		return mTableView;
	}

	
	AsyncTaskCompleteListener getMyOrderListener = new AsyncTaskCompleteListener(){

		@Override
		public void excuteHttpTask() {
			String url = Util.getUrl("MyOrderList");
			
			ArrayList<NameValuePair> value = new ArrayList<NameValuePair>();
			value.add(new BasicNameValuePair("USER_ID", CafeNoaPreference.getUserID(mContext)));
			
			HttpPostTask http = new HttpPostTask(mContext, value, url, getMyOrderListener);
			http.execute();
		}

		@Override
		public void onTaskComplete(String result, boolean taskOk) {
			if(taskOk){
				int code = -1;
				
				try {
					JSONObject object = new JSONObject(result);
					code = object.getInt("RESULT");

					if(code==Constants.NETWORK_SUCCESS){
						JSONArray jArr = object.getJSONArray("ITEM");
						if(jArr.length()==0){
							noList.setVisibility(View.VISIBLE);
							pb.setVisibility(View.GONE);
						} else {
							mTableView.setVisibility(View.VISIBLE);
							pb.setVisibility(View.GONE);
						}
						
						int orderNumber = -1;
						String content = null;
						String comment = null;
						String pickup = null;
						String method = null;
						int price = -1;						
						
						for(int i=0;i<jArr.length();i++){
							JSONObject obj = jArr.getJSONObject(i);
							orderNumber= obj.getInt("MYORDER_NUMBER");
							content = obj.getString("MYORDER_CONTENT");
							comment = obj.getString("MYORDER_COMMENT");
							pickup = obj.getString("MYORDER_PICKUP_TIME");
							method = obj.getString("MYORDER_PAYMENT_METHOD");
							price = obj.getInt("MYORDER_PRICE");
						
							list.add(new OrderList(orderNumber, content, comment, pickup, method, price));
							mTableView.addBasicItem("주문번호 : "+String.valueOf(orderNumber));
						}
						mTableView.commit();
						mTableView.setVisibility(View.VISIBLE);
						pb.setVisibility(View.GONE);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
		
	};
	private class CustomClickListener implements ClickListener {
		@Override
		public void onClick(final int index) {
			AlertDialog.Builder ab = new AlertDialog.Builder(mContext);
			ab.setTitle("내 주문내역");
			String content = list.get(index).getContent();
			content = content.replace(" ", "\n");
			ab.setMessage(content+"\n주문가격 : "+list.get(index).getPrice()+"원");
			ab.setPositiveButton("확인", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					
				}
			});
			
			AlertDialog ad = ab.create();
			ad.show();
			

		}
	}
	
	private class OrderList{
		int number;
		String content;
		String comment;
		String pickup_time;
		String method;
		int price;
		
		public OrderList(int number, String content, String comment, String pickup_time, String method, int price){
			this.number = number;
			this.content = content;
			this.comment = comment;
			this.pickup_time = pickup_time;
			this.method = method;
			this.price = price;
			
		}
		
		public int getNumber() {
			return number;
		}
		public void setNumber(int number) {
			this.number = number;
		}
		public String getContent() {
			return content;
		}
		public void setContent(String content) {
			this.content = content;
		}
		public String getComment() {
			return comment;
		}
		public void setComment(String comment) {
			this.comment = comment;
		}
		public String getPickup_time() {
			return pickup_time;
		}
		public void setPickup_time(String pickup_time) {
			this.pickup_time = pickup_time;
		}
		public String getMethod() {
			return method;
		}
		public void setMethod(String method) {
			this.method = method;
		}
		public int getPrice() {
			return price;
		}
		public void setPrice(int price) {
			this.price = price;
		}
	}
}
